﻿using System.Web.UI;
using ReadyApps.Common;

namespace ReadyApps.Models.Wiki
{
    class WikiPageRequest : IWikiPageRequest
    {
        #region Properties

        public string SiteName { get; private set; }

        public string BaseFolder { get; private set; }

        public string RequestedFolder { get; private set; }

        public string RequestedPage { get; private set; }

        public string CurrentUsername { get; private set; }

        #endregion

        #region Constructor

        public WikiPageRequest(string siteName, string baseFolder, string username, string pathData)
        {
            SiteName = siteName;
            BaseFolder = baseFolder;
            CurrentUsername = username;
            // Page was requested directly or the route maps to the base page
            if (string.IsNullOrEmpty(pathData))
            {
                RequestedFolder = "/";
                RequestedPage = ReadyAppsSettings.DEFAULT_PAGE_NAME;
            }
            else if (pathData.EndsWith("/")) // Ends with a '/' - folder request
            {
                RequestedFolder = "/" + pathData;
                RequestedPage = ReadyAppsSettings.DEFAULT_PAGE_NAME;
            }
            else // Has a page name attached to the end
            {
                int lastIndexOfSlash = pathData.LastIndexOf('/');
                // Folder included
                if (lastIndexOfSlash > 0)
                {
                    RequestedFolder = "/" + pathData.Substring(0, lastIndexOfSlash + 1);
                    RequestedPage = pathData.Substring(lastIndexOfSlash + 1);
                }
                else // Folder not included
                {
                    RequestedFolder = "/";
                    RequestedPage = pathData;
                }
            }
        }

        #endregion

        #region Public Methods

        public string GetFullPath()
        {
            string fullPath = BaseFolder;
            if (fullPath.EndsWith("/"))
            {
                fullPath += RequestedFolder.TrimStart('/');
            }
            else
            {
                fullPath += RequestedFolder;
            }
            return fullPath;
        }

        #endregion
    }
}
